package com.ruoyi.ahs.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.ahs.cache.AhsCache;
import com.ruoyi.ahs.service.IRushService;
import com.ruoyi.ahs.util.AhsUtils;
import com.ruoyi.ahs.util.RushTaskDto;
import com.ruoyi.ahs.util.WeChatBotMessageSender;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.system.domain.RushConfig;
import com.ruoyi.system.domain.ahs.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RushTaskService
 *
 * @author 辛凤文
 * @since 1.0
 */
@Slf4j
@Service
public class RushService implements IRushService {


    /**
     * 抢购商品
     * */
    @Override
    public void rushGoods(RushTaskDto task) {
        ProductSearch productSearch = task.getProductSearch();
        SearchResult searchResult = AhsUtils.search(productSearch);
        if (searchResult.isSuccess()) {
            List<Product> productList = searchResult.getData();
            if (CollectionUtils.isNotEmpty(productList)) {
                productList.forEach(product -> {
                    OrderResult orderResult = this.createOrder(product);
                    if (orderResult.isSuccess()) {
                        WeChatBotMessageSender.sendMessage(String.format("抢购商品成功！订单号:%s", orderResult.getData()),
                                null);
                    } else {
                        WeChatBotMessageSender.sendMessage(String.format("抢购失败！原因:%s", orderResult.getResultMessage()), 1);
                    }
                });
            }
        } else {
            WeChatBotMessageSender.sendMessage("搜索商品失败" + JSONObject.toJSONString(searchResult));
            throw new BaseException("搜索商品失败{}", JSONObject.toJSONString(searchResult));
        }
    }

    @Override
    public void search(ProductSearch productSearch) {

    }

    /**
     * 商品下单
     * */
    private OrderResult createOrder(Product product) {
        //优惠券
        List<Discount> discountList = product.getDiscountList();
        String discountCode = null;
        if (CollectionUtils.isNotEmpty(discountList)) {
            for (Discount discount : discountList) {
                if ("1001".equals(discount.getDiscountType())) {
                    discountCode = discount.getCode();
                    break;
                }
            }
            if (discountCode == null) {
                discountCode = discountList.get(0).getCode();
            }
        }
        Sensors sensors = AhsCache.getSensors();
        RushConfig config = AhsCache.getCurrentConfig();
        Order orderDTO = new Order();
        orderDTO.setProductSerialNo(product.getProductNo());
        orderDTO.setCityId(config.getCityId());
        orderDTO.setActivityCode(discountCode != null ? discountCode : config.getActivityCode());
        orderDTO.setPickupType(config.getPickupType());
        orderDTO.setAddressId(config.getAddressId() != null ? config.getAddressId() : AhsCache.getAddressId());
        orderDTO.setSensors(sensors);
        OrderResult order = AhsUtils.createOrder(orderDTO);
        return order;
    }
}
